<script setup>
import {onMounted, ref, reactive} from "vue";
import {httpGet, httpPost} from "@/utils/http.js";
import {ElMessage, ElMessageBox} from "element-plus";
import {dateFormat, removeArrayItem} from "@/utils/libs.js";
import {Plus} from "@element-plus/icons-vue";

const items = ref([])
const item = ref({})
const showDialog = ref(false)
const loading = ref(true)
const formRef = ref(null)

const rules = reactive({
  username: [{required: true, message: '请输入用户名', trigger: 'change',}],
  password: [{required: true, message: '请输入密码', trigger: 'change',}],
  repass: [{required: true, message: '请再次输入密码', trigger: 'change',}],
})

// 获取数据
const fetchData = () => {
  loading.value = true
  httpGet('api/admin/list').then(res => {
    items.value = res.data
    loading.value = false
  }).catch(e => {
    ElMessage.error('获取数据失败：' + e.message)
    loading.value = false
  })
}

onMounted(() => {
  fetchData()
})

// 点击新增触发
const add = () => {
  showDialog.value = true
  item.value = {}
}

// 重置密码
const resetPass = (row) => {
  ElMessageBox.prompt('请输入密码', '重置密码', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  }).then(({value}) => {
    httpPost('api/admin/resetPass', {id: row.id, password: value}).then(() => {
      ElMessage.success('操作成功！')
    }).catch(e => {
      ElMessage.error('操作失败：' + e.message)
    })
  }).catch(() => {
  })
}

const save = () => {
  formRef.value.validate((valid) => {
    if (item.value.password !== item.value.repass) {
      return ElMessage.error('两次输入密码不一致！')
    }
    if (valid) {
      httpPost('api/admin/save', item.value).then(() => {
        ElMessage.success('操作成功！')
        fetchData()
        showDialog.value = false
      }).catch(e => {
        ElMessage.error('操作失败：' + e.message)
      })
    } else {
      return false
    }
  })
}

const enable = (row) => {
  httpPost('api/admin/enable', {id: row.id, enabled: row.status}).then(() => {
    ElMessage.success('操作成功！')
  }).catch(e => {
    ElMessage.error('操作失败：' + e.message)
  })
}

const remove = (row) => {
  httpGet('api/admin/remove?id=' + row.id).then(() => {
    ElMessage.success('删除成功！')
    items.value = removeArrayItem(items.value, row, (v1, v2) => {
      return v1.id === v2.id
    })
  }).catch(e => {
    ElMessage.error('操作失败：' + e.message)
  })
}

</script>

<template>
  <div class="container list" v-loading="loading">
    <div class="handle-box">
      <el-button type="primary" :icon="Plus" @click="add">新增</el-button>
    </div>
    <el-row>
      <el-table :data="items" :row-key="row => row.id" table-layout="auto">
        <el-table-column prop="username" label="用户名"/>
        <el-table-column prop="last_login_ip" label="最后登录IP"/>
        <el-table-column label="最后登录时间">
          <template #default="scope">
            <span>{{ dateFormat(scope.row['last_login_at']) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="启用状态" prop="enabled">
          <template #default="scope">
            <el-switch v-model="scope.row['status']" @change="enable(scope.row)"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template #default="scope">
            <span>{{ dateFormat(scope.row['created_at']) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" type="primary" @click="resetPass(scope.row)">重置密码</el-button>
            <el-popconfirm title="确定要删除当前记录吗?" @confirm="remove(scope.row)" :width="200">
              <template #reference>
                <el-button size="small" type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-dialog v-model="showDialog" title="添加用户" :close-on-click-modal="false">
      <el-form :model="item" label-width="120px" ref="formRef" :rules="rules">
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="item.username" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model="item.password" type="password" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="重置密码：" prop="repass">
          <el-input v-model="item.repass" type="password" autocomplete="off"/>
        </el-form-item>
      </el-form>
      <template #footer>
            <span class="dialog-footer">
              <el-button @click="showDialog = false">取消</el-button>
              <el-button type="primary" @click="save">提交</el-button>
            </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="stylus">
.list {

  .handle-box {
    margin-bottom 20px
  }

  .el-select {
    width: 100%
  }

  .pagination {
    padding 20px 0
    display flex
    justify-content right
  }
}
</style>